﻿package lobby
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import lobby.events.LobbyEvent;
	import lobby.constants.ConstData;
	
	/**
	 * ...
	 * @author Tom
	 */
	
	public class TableMC extends MovieClip
	{
		private var casinoId:String;
		private var gameTypeId:String;
		public var txtName:TextField;
		public var content:MovieClip;
		public var tbMask:MovieClip;
		public var isClosed:Boolean;
		
		public function TableMC()
		{
			isClosed = false;
			tbMask.alpha = 0;
			tbMask.buttonMode = true;
			tbMask.addEventListener(MouseEvent.ROLL_OVER, hdlMouseEvent);
			tbMask.addEventListener(MouseEvent.ROLL_OUT, hdlMouseEvent);
			tbMask.addEventListener(MouseEvent.CLICK, hdlMouseEvent);
			this.cacheAsBitmap = true;
			this.tabChildren = false;
		}
		
		public function set setCasinoId(casinoId:String)
		{
			this.casinoId = casinoId;
		}
		
		public function set setGameTypeId(gameTypeId:String)
		{
			this.gameTypeId = gameTypeId;
		}
		
		public function get getGameTypeId():String
		{
			return this.gameTypeId;
		}
		
		private function hdlMouseEvent(e:MouseEvent):void
		{
			switch (e.type)
			{
				case MouseEvent.ROLL_OVER: 
					tbMask.alpha = 0.5;
					break;
				case MouseEvent.ROLL_OUT: 
					tbMask.alpha = 0;
					break;
				case MouseEvent.CLICK: 
					openTable();
					break
			}
		}
		
		private function openTable():void
		{
			var e:LobbyEvent = new LobbyEvent(LobbyEvent.OPEN_SELECTED_TABLE, true);
			e.tableId = this.name;
			e.casinoID = this.casinoId;
			dispatchEvent(e);
		}
		
		public function changeTextLang():void
		{
			if (!this.isClosed)
			{
				var str:String = txtName.text;
				var pos:int = str.lastIndexOf(" ");
				txtName.text = getGameTypeName();
				if (gameTypeId != ConstData.GAME_TYPE_BAC3)
				{
					txtName.appendText(str.substring(pos, str.length));
				}
				
				var panel:HistoryPanelBase;
				var obj:DisplayObject = content.getChildByName(this.name);
				switch (this.gameTypeId)
				{
					case ConstData.GAME_TYPE_BAC: 
					case ConstData.GAME_TYPE_BAC_IN: 
						panel = HistoryPanelBaccarat(obj);
						panel.initBoard(panel.getHistory);
						break;
					case ConstData.GAME_TYPE_DT: 
						panel = HistoryPanelDragonTiger(obj);
						panel.initBoard(panel.getHistory);
						break;
					case ConstData.GAME_TYPE_SICBO: 
						panel = HistoryPanelSicbo(obj);
						panel.initBoard(panel.getHistory);
						break;
				}
			}
			else
			{
				var mc:MovieClip = MovieClip(MovieClip(content.getChildByName(this.name)).getChildByName("tableclosed"));
				mc["tableclose_txt"].text = GlobalVariables.langInfo.hashTable.lookupString("INFOclosetable");
			}
		}
		
		public function getGameTypeName():String
		{
			var gameTypeName:String = "";
			switch (gameTypeId)
			{
				case ConstData.GAME_TYPE_BAC: 
					var tbId:int = int(this.name);
					if (56 <= tbId && tbId <= 60)
					{
						gameTypeName = GlobalVariables.langInfo.hashTable.lookupString("bactele");
					}
					else
					{
					gameTypeName = GlobalVariables.langInfo.hashTable.lookupString("bac");
					}
					break;
				case ConstData.GAME_TYPE_BAC_IN: 
					gameTypeName = GlobalVariables.langInfo.hashTable.lookupString("baccaratins");
					break;
				case ConstData.GAME_TYPE_BAC3: 
					gameTypeName = GlobalVariables.langInfo.hashTable.lookupString("3bac");
					break;
				case ConstData.GAME_TYPE_DT: 
					gameTypeName = GlobalVariables.langInfo.hashTable.lookupString("dragon");
					break;
				case ConstData.GAME_TYPE_FANTAN: 
					gameTypeName = GlobalVariables.langInfo.hashTable.lookupString("fantan");
					break;
				case ConstData.GAME_TYPE_ROULETTE: 
					gameTypeName = GlobalVariables.langInfo.hashTable.lookupString("roulette");
					break;
				case ConstData.GAME_TYPE_SEDIE: 
					gameTypeName = GlobalVariables.langInfo.hashTable.lookupString("sedie");
					break;
				case ConstData.GAME_TYPE_SICBO: 
					gameTypeName = GlobalVariables.langInfo.hashTable.lookupString("dice");
					break;
			}
			return gameTypeName;
		}
	}

}
